#cmake file for auxiliary tools
#author:guyadong
#created:2018/05/30

# 查找jpegwrapper库,需要设置 CMAKE_PREIX_PATH 指定 jpegwrapper,turbojpeg,openjpeg的安装路径
# 需要指定CMAKE_MODULE_PATH 指定 FindTurboJPEG.cmake 的位置
# 本例中的位置在项目根目录下/cmake/Modules
find_package(jpegwrapper REQUIRED CONFIG)

set(AUX_LIB_NAME gfaux_tools)
aux_source_directory(${CMAKE_CURRENT_LIST_DIR} _AUX_SOURCES)
file(GLOB _AUX_HEADERS RELATIVE ${CMAKE_CURRENT_LIST_DIR} "${CMAKE_CURRENT_LIST_DIR}/*.h")
set(_public_headers)
list(APPEND _public_headers ${_AUX_HEADERS})
# 用于外部调用的公共头文件
list (APPEND _public_headers
    "${DEPENDENT_SOURCE_DIR}/image_matrix_types.h"
    )
list(APPEND _AUX_SOURCES "${DEPENDENT_SOURCE_DIR}/image_matrix_types.cpp")
set(_aux_targets)
if(MSVC)
	if(TARGET faceapiclient_mt)
		message("-- create static target: ${AUX_LIB_NAME}_mt")
		add_library(${AUX_LIB_NAME}_mt STATIC ${_AUX_SOURCES} )
		with_mt_if_msvc(${AUX_LIB_NAME}_mt)
		list(APPEND _aux_targets ${AUX_LIB_NAME}_mt)
	endif()
	if(TARGET faceapiclient_md)
		message("-- create static target: ${AUX_LIB_NAME}_md")
		add_library(${AUX_LIB_NAME}_md STATIC ${_AUX_SOURCES} )
		list(APPEND _aux_targets ${AUX_LIB_NAME}_md)
	endif()
else()
	message("-- create static target: ${AUX_LIB_NAME}")
	add_library(${AUX_LIB_NAME} STATIC ${_AUX_SOURCES} )
	list(APPEND _aux_targets ${AUX_LIB_NAME})
endif(MSVC)

foreach(_target ${_aux_targets})
# 根据target名字后缀计算 thrift_static target后缀名
string(REGEX MATCH "_m[td]$" _static_postfix "${_target}")

# MSVC Debug 编译时生成包含用于调试器的完整符号调试信息的 .obj 文件
# see also https://msdn.microsoft.com/zh-cn/library/958x11bc.aspx
target_compile_options(${_target} PRIVATE $<$<AND:$<CXX_COMPILER_ID:MSVC>,$<CONFIG:Debug>>:/Z7> $<$<CXX_COMPILER_ID:MSVC>:/wd4251 /wd4275>)
target_compile_definitions(${_target} 
	        PUBLIC _SL_USE_BYTE_STREAM
					)
target_include_directories (${_target}
	INTERFACE "$<INSTALL_INTERFACE:include>"
			"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>"
			"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/..>"
			"$<BUILD_INTERFACE:${DEPENDENT_SOURCE_DIR}>"
	)
set_target_properties (${_target}  PROPERTIES 
	PUBLIC_HEADER "${_public_headers}"
	VERSION ${PROJECT_VERSION} 
	SOVERSION ${PROJECT_VERSION_MAJOR}
	FOLDER "auxiliary"
	)
set_target_properties (${_target}  PROPERTIES 
	DEBUG_POSTFIX _d
	)

set(_link_libraries faceapiclient${_static_postfix} gdface::jpegwrapper-static $<$<PLATFORM_ID:Linux>:-lX11> $<$<CXX_COMPILER_ID:GNU>:-lpthread>)

target_link_libraries(${_target} PUBLIC ${_link_libraries})	
target_compile_definitions(${_target} 
		PUBLIC GFAUX_STATIC
				)
set_target_properties (${_target}  PROPERTIES INTERFACE_POSITION_INDEPENDENT_CODE ON
														POSITION_INDEPENDENT_CODE ON)
target_link_libraries(${_target} PRIVATE $<BUILD_INTERFACE:common_source> ) 

endforeach(_target)
################安装脚本#################
#install(TARGETS ${_aux_targets} RUNTIME DESTINATION ${RUNTIME_INSTALL_DIR} )
# ----------------------------------------------------------------------------
include (CMakePackageConfigHelpers)
set (aux_DEPENDENCY "find_dependency(jpegwrapper)")
configure_package_config_file (${PROJECT_SOURCE_DIR}/cmake/config-aux.cmake.in
  ${CMAKE_CURRENT_BINARY_DIR}/${AUX_LIB_NAME}-config.cmake
  INSTALL_DESTINATION ${CONFIG_INSTALL_DIR}
  NO_CHECK_REQUIRED_COMPONENTS_MACRO)
write_basic_package_version_file (${AUX_LIB_NAME}-config-version.cmake VERSION
  ${PROJECT_VERSION} COMPATIBILITY SameMajorVersion)
################安装脚本#################
install(TARGETS ${_aux_targets} EXPORT ${AUX_LIB_NAME}-targets
  RUNTIME DESTINATION ${RUNTIME_INSTALL_DIR}
  LIBRARY DESTINATION ${LIBRARY_INSTALL_DIR}
  ARCHIVE DESTINATION ${LIBRARY_INSTALL_DIR}
  PUBLIC_HEADER DESTINATION ${INCLUDE_INSTALL_DIR}/${AUX_LIB_NAME}
  INCLUDES DESTINATION ${INCLUDE_INSTALL_DIR}/${AUX_LIB_NAME}
  )
install (FILES
  ${CMAKE_CURRENT_BINARY_DIR}/${AUX_LIB_NAME}-config.cmake
  ${CMAKE_CURRENT_BINARY_DIR}/${AUX_LIB_NAME}-config-version.cmake
  DESTINATION ${CONFIG_INSTALL_DIR}
  )

install (EXPORT ${AUX_LIB_NAME}-targets NAMESPACE gdface:: DESTINATION ${CONFIG_INSTALL_DIR} )

unset(AUX_LIB_NAME)
unset(_aux_targets)
unset(_public_headers)